﻿
function $( objectId )
{
    return document.getElementById( objectId );
}

function HtmlEncode( value )
{
    var encoded = new String(value);
    
    encoded = encoded.replace(/&/g, "&amp;");
    encoded = encoded.replace(/</g, "&lt;");
    encoded = encoded.replace(/>/g, "&gt;");
    encoded = encoded.replace(/"/g, "&quot;");
    
    return encoded;
}

function InternetExplorerVersion()
{
    if ( IsInternetExplorer() )
    {
        if ( new RegExp( "MSIE ([0-9]{1,}[\.0-9]{0,})" ).exec( navigator.userAgent ) != null )
            return parseFloat( RegExp.$1 );
    }
    else
    {
        return 0; 
    }
}

function IsInternetExplorer()
{
    return ( navigator.userAgent.toLowerCase().indexOf("msie") != -1 );
}

function IsInternetExplorer7()
{
    return ( InternetExplorerVersion() >= 7  );
}

function CardSpaceAvailable( objectId )
{
    var installed = false;
    
    try
    {
        var cardSpace = $( objectId );
        
        if ( IsInternetExplorer() )
        {
            //
            // IE detection logic
            //
            if ( InternetExplorerVersion() >= 7 )
            {
                if ( cardSpace.isInstalled )
                {
                    installed = true; 
                }
            }
        }
        else 
        {
            //
            // Non-IE detection logic
            //
            if ( navigator.mimeTypes && navigator.mimeTypes.length )
            {
                var x = navigator.mimeTypes['application/x-informationcard'];
                
                if ( x && x.enabledPlugin )
                    installed = true;
                else
                {
                    //    
                    // Check for the IdentitySelector event handler
                    //
                    var event = document.createEvent("Events"); 
                    
                    event.initEvent("IdentitySelectorAvailable", true, true);
                    
                    top.dispatchEvent( event ); 

                    if ( top.IdentitySelectorAvailable == true )
                        installed = true; 
                }
            }
        }
    }
    catch (e)
    {
        return false;
    }
    
    return installed;
}
